package com.hu.jie.mywallpager.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.hu.jie.mywallpager.R;
import com.hu.jie.mywallpager.activity.HomeActivity;
import com.hu.jie.mywallpager.activity.JumpActivity;
import com.hu.jie.mywallpager.activity.PageActivity;
import com.hu.jie.mywallpager.bean.BeanType1;
import com.hu.jie.mywallpager.contanse.UrlString;
import com.hu.jie.mywallpager.utils.VolleyUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class TypeFragment extends Fragment {

    private ListView lv;
    private List<BeanType1.MyData> mlist;
    private TextView tv;
    private MyAdapter adapter;

    public TypeFragment() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mlist = new ArrayList<>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_type, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv = (TextView) view.findViewById(R.id.textView);
        tv.setText("分类");
        lv = (ListView) view.findViewById(R.id.listview);

        VolleyUtils.newGsonRequest(Request.Method.GET, UrlString.URL_TYPE, new Response.Listener<BeanType1>() {
            @Override
            public void onResponse(BeanType1 response) {
                mlist.addAll(response.data);
                Log.i("mlist","!!----->!!!!-------->!!!"+mlist.size());
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        },BeanType1.class);
        adapter = new MyAdapter();
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String url_id = mlist.get(position).ID;
                if (position!=0){
                    Toast.makeText(getContext(), "等等"+position, Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getContext(), "无数据", Toast.LENGTH_SHORT).show();
                }
                Intent intent = new Intent(getContext(), PageActivity.class);
                Bundle b = new Bundle();
                b.putString("title",mlist.get(position).PicCategoryName);
                b.putString("home",UrlString.URL_TYPE_HOME + url_id);
                b.putString("hot",UrlString.URL_TYPE_HOT + url_id);
                b.putString("random",UrlString.URL_TYPE_RANDOM + url_id);
                intent.putExtras(b);
                startActivity(intent);
            }
        });
    }




    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            Log.i("mlist","!!----->!!!!-------->!!!"+mlist.size());
            return mlist.size();
        }

        @Override
        public Object getItem(int position) {
            return mlist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            RelativeLayout rl = null;
            ViewHolder holder = null;
            if (convertView==null){
                holder = new ViewHolder();
                rl = (RelativeLayout) getActivity().getLayoutInflater().inflate(R.layout.layout_type,parent,false);
                holder.niv = (NetworkImageView) rl.findViewById(R.id.image1);
                holder.iv = (ImageView) rl.findViewById(R.id.image2);
                holder.tv1 = (TextView) rl.findViewById(R.id.textViewty);
                holder.tv2 = (TextView) rl.findViewById(R.id.textty2);
                rl.setTag(holder);
            }else {
                rl = (RelativeLayout) convertView;
                holder = (ViewHolder) rl.getTag();
            }
            holder.niv.setImageUrl(mlist.get(position).CategoryPic, VolleyUtils.newInstance(getContext()).getLoader());
            holder.tv1.setText(mlist.get(position).PicCategoryName);
            holder.tv2.setText(mlist.get(position).DescWords);
            holder.iv.setImageResource(R.mipmap.arrow);
            return rl;
        }

        private class ViewHolder {
            private NetworkImageView niv;
            private TextView tv1;
            private TextView tv2;
            private ImageView iv;
        }
    }


}
